(function() {

  'use strict';

  angular.module('controllers.faultLocation.pbossOm')
    .controller('hostResourceController', hostResourceController);

  function hostResourceController($scope, COREDATA) {
    var socket = io.connect('http://' + COREDATA.SOCKETIP);
    var ramEchart = echarts.init(document.getElementById('ram'));
    var diskEchart = echarts.init(document.getElementById('disk'));
    var networkEchart = echarts.init(document.getElementById('network'));
    var cpuEchart = echarts.init(document.getElementById('cpu'));
    requaret();

    function requaret() {
      var cpuOption = echartsInstance('CPU', '百分比');
      var ramOption = echartsInstance('内存', '百分比');
      var diskOption = echartsInstance('磁盘', 'kb/s');
      var newWorkOption = echartsInstance('网络', 'Kbps');
      cpuEchart.setOption(cpuOption);
      ramEchart.setOption(ramOption);
      diskEchart.setOption(diskOption);
      networkEchart.setOption(newWorkOption);
      socket.on('sysSourceUseRate', function(result) {
        ramEchart.addData([
          [0, result.ram, false, false]
        ]);
        cpuEchart.addData([
          [0, result.cpu, false, false]
        ]);
        diskEchart.addData([
          [0, result.disk, false, false]
        ]);
        networkEchart.addData([
          [0, result.network, false, false]
        ]);
      });
    }

    function echartsInstance(sname, yname) {
      var option = {};
      option.tooltip = {
        trigger: 'axis',
        formatter: function(params) {
          var param = params[0];
          return param.seriesName;
        }
      };
      // option.title = {
      //   text: titleName
      // };
      option.calculable = false;
      option.xAxis = [{
        type: 'category',
        // boundaryGap: true,
        data: (function() {
          var res = ['60秒'];
          var len = 60;
          while (len--) {
            res.push('');
          }
          res[res.length - 1] = '0';
          return res;
        })()
      }];
      option.yAxis = [{
        type: 'value',
        name: yname,
        splitNumber: 4
      }];
      option.series = [{
        type: 'line',
        name: sname,
        symbol: 'none',
        data: (function() {
          var res = [];
          var len = 60;
          while (len--) {
            res.push(0);
          }
          return res;
        })(),
        itemStyle: {
          normal: {
            areaStyle: {
              type: 'default'
            }
          }
        }
      }];
      return option;
    }
  }
  hostResourceController.$inject = ['$scope', 'COREDATA'];
})();
